package br.com.pfc.mobile.comunicao;

import java.util.Vector;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;
import br.com.pfc.mobile.transferobject.CondicaoPagamentoTO;

public class ComunicacaoCondicaoPagamento extends Comunicacao{

	private static final String METHOD_LISTARCONDICAOPAGAMENTO = "listarCondicaoPagamento";
	private static final String SOAP_ACTION =  "http://webservice.siac/";
	
	public CondicaoPagamentoTO[] listarCondicaoPagamento(){
		/** TODO */
		getSoapObject(METHOD_LISTARCONDICAOPAGAMENTO);
				
		SoapSerializationEnvelope envelope = getEnvelope();
		HttpTransportSE transport = new HttpTransportSE(URL);
		CondicaoPagamentoTO[] cpsTO = null;
		try {
			//chamando o webservice
			transport.call(SOAP_ACTION + METHOD_LISTARCONDICAOPAGAMENTO, envelope);
			//pegando o retorno
			if(envelope.getResponse() instanceof SoapObject){
				SoapObject response = (SoapObject) envelope.getResponse();
				if(response != null){
					CondicaoPagamentoTO cpTO = new CondicaoPagamentoTO();
					cpTO.setCodigo(Integer.parseInt(response.getProperty(CondicaoPagamentoTO.PROP_CODIGO).toString()));
					cpTO.setDescricao(response.getProperty(CondicaoPagamentoTO.PROP_DESCRICAO).toString());
					cpTO.setEntrada(Boolean.parseBoolean(response.getProperty(CondicaoPagamentoTO.PROP_ENTRADA).toString()));
					cpTO.setNumeroParcelas(Integer.parseInt(response.getProperty(CondicaoPagamentoTO.PROP_NUMERO_PARCELAS).toString()));
					
					cpsTO[0] = cpTO;	
				}
				
			}else{
				
				Vector<SoapObject> response = (Vector<SoapObject>) envelope.getResponse();
				if(response != null){
					cpsTO = new CondicaoPagamentoTO[response.size()];

					for (int i = 0; i < cpsTO.length; i++) {
						SoapObject soapCP = (SoapObject)response.get(i);

						CondicaoPagamentoTO cpTO = new CondicaoPagamentoTO();
						cpTO.setCodigo(Integer.parseInt(soapCP.getProperty(CondicaoPagamentoTO.PROP_CODIGO).toString()));
						cpTO.setDescricao(soapCP.getProperty(CondicaoPagamentoTO.PROP_DESCRICAO).toString());
						cpTO.setEntrada(Boolean.parseBoolean(soapCP.getProperty(CondicaoPagamentoTO.PROP_ENTRADA).toString()));
						cpTO.setNumeroParcelas(Integer.parseInt(soapCP.getProperty(CondicaoPagamentoTO.PROP_NUMERO_PARCELAS).toString()));

						cpsTO[i] = cpTO;
					}
				}		
						
				
			}
			
			
			
			
		} catch (Exception e) {
			Log.i("PFC","erro no ws: " + e.getMessage());
		}
		return cpsTO;
	
	}
}
